<template>
  <div class="parkingContainer">
    <div class="search">
      <el-input v-model.trim="searchValue" placeholder="请输入区域关键字进行查询" clearable :prefix-icon="Search" @keyup="getParkingList(searchValue)">
        <template #append>
          <el-button :icon="Search" @click="getParkingList(searchValue)"/>
        </template>
      </el-input>
    </div>
    <el-table :data="parkingData" stripe style="width: 100%;margin-top: 10px;" height="50%">
    <el-table-column prop="parkingId" label="车位ID" width="85"/>
    <el-table-column prop="areaId" label="区域ID" width="85"/>
    <el-table-column prop="parkingState" label="车位状态" :filters="[
        { text: '占有', value: '占有' },
        { text: '空闲', value: '空闲' },]" :filter-method="filterState"  filter-placement="bottom-end">
      <template #default="scope">
        <el-tag :type="scope.row.parkingState === '占有' ? '' : 'success'"  disable-transitions >{{ scope.row.parkingState }}</el-tag>
      </template>   
    </el-table-column>
    <el-table-column prop="name" label="区域名称" :filters="nameFilter" :filter-method="filterHandler">
    </el-table-column>
    <el-table-column prop="location" label="区域位置" />
  </el-table>
   <div class="pagination">
    <el-pagination v-model:current-page="page" v-model:page-size="size" :page-sizes="[5,10,20,30]" :small="small" :disabled="disabled"
      :background="background" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
  </div>
  </div>
</template>

<script setup>
import { reactive , inject , onMounted , ref } from "vue"
import axios from '../../axios/axiosInstance'
import { ElLoading,ElMessage } from 'element-plus'
import {Search} from '@element-plus/icons-vue'

//变量定义
let searchValue = ref()
let nameFilter = reactive([])
let parkingData = reactive([])
const Parking = reactive({
  parkingId: "",
  areaId: "",
  parkingState: "",
  name: "",
  location: "",
})
const total = ref(0)
let page = 1
let size = 5
const small = ref(false)
const background = ref(false)
const disabled = ref(false)

//方法

const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  size = val
  init()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  page = val
  init()
}

const clear = (value) => {
    Object.keys(value).map(key => {
      delete value[key]
    })
  }

const filterHandler = (value,row,column) => {
  const property = column['property']
  return row[property] === value
}

const filterState = (value, row) => {
  return row.parkingState === value
}

const getParkingList = (searchValue) => {
  if(searchValue != ""){ 
    axios.get(`/parking-service/parking-lot/parkingList/${searchValue}`).then((res) => {   
      console.log("res为：")
      console.log(res)
       if(res.code == 200){
          clear(parkingData)
          for(let i in res.data){
            console.log(i)
            parkingData.push(res.data[i])
          }
          ElMessage({type: "success",message: "查询成功"});
        }else{
          ElMessage({type: "error",message: "查询失败"});
       }
    }).catch((err) => {
      console.log(err)
    })
  }else{
    init()
  }
}

const init = () => {
  const loading = ElLoading.service({
        lock: true,
        text: '拼命加载中',
        background: 'rgba(0, 0, 0, 0.7)',
      })
      setTimeout(() => {
        loading.close()
      }, 2000)
     axios.get(`/parking-service/parking-lot/parkingList/${page}/${size}`).then((res) => {   
      console.log("res为：")
      console.log(res)
      clear(parkingData)
      total.value = res.data.total
      console.log(total)
      for(let i in res.data.content){
        console.log(i)
        parkingData.push(res.data.content[i])
      }
      console.log("最后的parkingData是：")
      console.log(parkingData)
    }).catch((err) => {

      console.log(err)

    })
}

const getNameFilter = () => {
   axios.get('/parking-service/area/areaName').then((res) => {   
      console.log("res为：")
      console.log(res)
      console.log(total)
      for(let i in res.data){
        console.log(i)
        nameFilter.push(res.data[i])
      }
      console.log("最后的parkingData是：")
      console.log(nameFilter)
    }).catch((err) => {

      console.log(err)

    })
}

//页面初始化方法
onMounted(()=>{ 
  init()
  getNameFilter()
})
</script>

<style scoped>
.parkingContainer .search{
  display: flex;
  width: 600px;
}
.parkingContainer .pagination{
  justify-content: center;
 margin-top: 15px;
}

</style>
